import React from 'react';
import PropTypes from 'prop-types';
import {Card, Button, Row, Col, Divider} from 'antd';
import {DATETIME_FORMAT} from '../constants';
import {getDetail, abandonData} from './api';
import {conventEnumValueToString, formatDateTime} from '../utils';
import {salesLeadSourceType, salesLeadIntentLevel, salesLeadInformationSource, salesLeadPurchaseBudget, gender} from '../Enum';
import {hasPermissions} from './utils';
import {PERMISSION} from './constants';
import DescriptionList from 'ant-design-pro/lib/DescriptionList';
import WrappedPopconfirm from '../common/WrappedPopconfirm';
import routes from './routes';
import styles from './style.css';
const {Description} = DescriptionList;

export class DetailPanel extends React.PureComponent {
    state = {
        loading: false,
        data: {},
    };
    componentDidMount() {
        this.getDetailInfo();
    }
    getDetailInfo = () => {
        const id = this.props.id;
        this.setState({loading: true});
        getDetail(id)
            .then(res => {
                if(res.ok)
                    this.setState({
                        loading: false,
                        data: res.data,
                    });
                    
                else
                    this.setState(({
                        loading: false
                    }));
            });
    }
    abandonConfirm = () => {
        this.setState({loading: true});
        const rowVersion = this.state.data.rowVersion;
        abandonData(this.props.id, rowVersion)
            .then(res => {
                this.setState({loading: false});
                if(res.ok)
                    this.props.successReturn();
            });
    }
    render() {
        const {data} = this.state;
        const {formatMessage} = this.props.intl;
        const operations = [];
        if(hasPermissions(this.props.permissions, PERMISSION.abandon) && hasPermissions(data.options, PERMISSION.abandon))
            operations.push(
                <WrappedPopconfirm
                    id="abandon"
                    key="abandon"
                    title={formatMessage({
                        id: 'detailPanel.popconfirm.message',
                        defaultMessage: '是否继续?'
                    })}
                    onConfirm={this.abandonConfirm}>
                    <Button key="abandon" size="small">{
                        formatMessage({
                            id: 'detailPanel.btn.abandon',
                            defaultMessage: '作废'
                        })
                    }</Button>
                </WrappedPopconfirm>);
        return (
            <div className="detail">
                <Card loading={this.state.loading}>
                    <DescriptionList size="large" title={<Row><Col span={12}>{
                        formatMessage({
                            id: 'detailPanel.card.title',
                            defaultMessage: '线索信息'
                        })
                    }</Col>
                    <Col className={`col-align-right ${styles.detail}`} span={12}>{operations}</Col>
                    </Row>}>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.code',
                            defaultMessage: '线索编号',
                        })}>{data.code}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.brandName',
                            defaultMessage: '品牌',
                        })}>{data.brandName}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.dealerCode',
                            defaultMessage: '总代',
                        })}>({data.dealerCode}){data.dealerName}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.sourceType',
                            defaultMessage: '线索来源',
                        })}>{conventEnumValueToString(salesLeadSourceType, data.sourceType)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.remark',
                            defaultMessage: '备注',
                        })}>{data.remark}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.creatorName',
                            defaultMessage: '创建人',
                        })}>{data.creatorName}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.createTime',
                            defaultMessage: '创建时间',
                        })}>{formatDateTime(data.createTime, DATETIME_FORMAT)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.modiferName',
                            defaultMessage: '修改人',
                        })}>{data.modiferName}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.modifyTime',
                            defaultMessage: '修改时间',
                        })}>{formatDateTime(data.modifyTime, DATETIME_FORMAT)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.abandonerName',
                            defaultMessage: '作废人',
                        })}>{data.abandonerName}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.abandonTime',
                            defaultMessage: '作废时间',
                        })}>{formatDateTime(data.abandonTime, DATETIME_FORMAT)}</Description>
                    </DescriptionList>
                    <Divider/>
                    <DescriptionList size="large" title={<Row><Col span={12}>{
                        formatMessage({
                            id: 'detailPanel.card.timeTitle',
                            defaultMessage: '客户信息'
                        })
                    }</Col>
                    </Row>}><Description term={formatMessage({
                            id: 'detailPanel.data.customerName',
                            defaultMessage: '客户姓名',
                        })}>{data.customerName}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.gender',
                            defaultMessage: '性别',
                        })}>{conventEnumValueToString(gender, data.gender)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.birthday',
                            defaultMessage: '出生年月',
                        })}>{formatDateTime(data.birthday, 'YYYY/MM')}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.contactNumber',
                            defaultMessage: '联系方式',
                        })}>{data.contactNumber}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.address',
                            defaultMessage: '联系地址',
                        })}>{data.address}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.purchaseBudget',
                            defaultMessage: '购车预算',
                        })}>{conventEnumValueToString(salesLeadPurchaseBudget, data.purchaseBudget)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.intentionModel',
                            defaultMessage: '拟购车型',
                        })}>{data.intentionModel}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.intentLevel',
                            defaultMessage: '意向级别',
                        })}>{conventEnumValueToString(salesLeadIntentLevel, data.intentLevel)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.informationSource',
                            defaultMessage: '信息来源',
                        })}>{conventEnumValueToString(salesLeadInformationSource, data.informationSource)}</Description>
                    </DescriptionList>
                </Card>
                <div className="page-toolbar">
                    <Button
                        type="primary"
                        disabled={this.state.loading}
                        onClick={this.props.onCancel}>{formatMessage({
                            id: 'detailPanel.btn.return',
                            defaultMessage: '返回'
                        })}</Button>
                </div>
            </div>
        );
    }
}

DetailPanel.propTypes = {
    id: PropTypes.string.isRequired,
    permissions: PropTypes.array.isRequired,
    onCancel: PropTypes.func.isRequired,
    history: PropTypes.object,
    intl: PropTypes.object,
    successReturn: PropTypes.func,
};

import {connect} from 'react-redux';
import {tableSearch} from './actions';
import {injectIntl} from './intl';

import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
const getPermissions = selectorFactory(['page', 'domainData', 'permission', 'data']);
const mapStateToProps = (state, props) => ({
    id: props.id,
    permissions: getPermissions(state),
});
const mapDispatchToProps = (dispatch, props) => ({
    onCancel: () => {
        props.history.push(routes.query.url());
    },
    successReturn: () => {
        dispatch(tableSearch());
        props.history.push(routes.query.url());
    },
});
export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(DetailPanel));
